<script setup lang="ts">
import { ref } from 'vue'
import { useCreateProject } from '../composables/useCreateProject'
import { baseUrl } from '../config'

const props = defineProps<{ companyId: number | null }>()
const emit = defineEmits(['created'])

const name = ref('')
const comment = ref('')
const aggregatePackage = ref('')
const enumPackage = ref('')
const languageType = ref(0)
const { createProject, loading, error, result } = useCreateProject(baseUrl)

async function handleSubmit() {
  if (!props.companyId || !name.value) return
  await createProject({
    companyId: props.companyId,
    name: name.value,
    comment: comment.value,
    aggregatePackage: aggregatePackage.value,
    enumPackage: enumPackage.value,
    languageType: languageType.value
  })
  if (result.value && result.value.id !== undefined) {
    emit('created')
    name.value = ''
    comment.value = ''
    aggregatePackage.value = ''
    enumPackage.value = ''
    languageType.value = 0
  }
}
</script>

<template>
  <div class="create-project-panel">
    <h3>创建项目</h3>
    <form @submit.prevent="handleSubmit">
      <div class="form-item">
        <label>项目名称：</label>
        <input v-model="name" required placeholder="请输入项目名称" />
      </div>
      <div class="form-item">
        <label>项目说明：</label>
        <input v-model="comment" placeholder="请输入项目说明" />
      </div>
      <div class="form-item">
        <label>聚合包名：</label>
        <input v-model="aggregatePackage" placeholder="请输入聚合包名" />
      </div>
      <div class="form-item">
        <label>枚举包名：</label>
        <input v-model="enumPackage" placeholder="请输入枚举包名" />
      </div>
      <div class="form-item">
        <label>语言类型：</label>
        <select v-model="languageType">
          <option :value="0">未知</option>
          <option :value="1">Java</option>
          <option :value="2">Csharp</option>
        </select>
      </div>
      <button type="submit" :disabled="loading || !props.companyId">创建</button>
    </form>
    <div v-if="error" class="error">{{ error }}</div>
    <div v-if="result" class="success">创建成功，ID: {{ result.id }}</div>
  </div>
</template>

<style scoped>
.create-project-panel {
  max-width: 400px;
  margin: 0 auto;
  padding: 24px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0,0,0,0.08);
}
.form-item {
  margin-bottom: 16px;
  display: flex;
  flex-direction: column;
}
label {
  margin-bottom: 4px;
  font-weight: bold;
}
input, select {
  padding: 8px;
  border-radius: 4px;
  border: 1px solid #ddd;
}
button {
  padding: 8px 24px;
  border: none;
  border-radius: 6px;
  background: #1890ff;
  color: #fff;
  font-size: 16px;
  cursor: pointer;
}
button:disabled {
  background: #bcdffb;
  cursor: not-allowed;
}
.error {
  color: #ff4d4f;
  margin-top: 8px;
}
.success {
  color: #52c41a;
  margin-top: 8px;
}
</style>
